<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 频道列表标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 频道列表里面的每个频道对应的页面 每循环一次都会创建一个 -->
        <AarticleList :channel="channel"></AarticleList>
      </van-tab>
      <!-- 汉堡组件前面的空格 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 通过插槽插入汉堡组件图标 -->
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="show = true"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="show"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 弹出层里面的选框组件 -->
      <Popup
        :active="active"
        :myChannels="channels"
        @update-active="onUpdateActive"
      ></Popup>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '../../api/user'
import AarticleList from './components/AarticleList.vue'
import Popup from './components/popup.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomePage',
  components: {
    AarticleList,
    Popup
  },
  props: {},
  data() {
    return {
      active: 0,
      channels: [],
      show: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created() {
    // 获取到频道列表导航栏的数据,然后渲染出来
    // 这个地方不用判token 因为后端已经处理了,有token就会获取到客户自己的列表,没有就获取的默认的列表
    this.loadChannels()
  },
  methods: {
    // 页面一打开就会触发
    async loadChannels() {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        let channels = []
        // 先判断是否登录了
        if (this.user) {
          // 如果登录了就获取服务器上用户的数据
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录 先查看本地有不有
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            // 本地有，直接用
            channels = localChannels
          } else {
            // 本地没有就用服务器默认的
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    onUpdateActive(index, show = true) {
      this.active = index
      this.show = show
    }
  }
}
</script>

<style scoped lang="less">
//  /deep/是深度作用域选择器,在scope里面父组件就想要修改子组件样式的时候,就用/deep/ 这里用的是less所以用的是这个,方式不一样写法不一样,在vue官网生态系统vue loader 里面的scoped css 里面有介绍
.home-container {
  padding-bottom: 100px;
  padding-top: 180px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  /deep/.van-tabs__wrap {
    position: fixed !important;
    top: 92px;
    z-index: 2;
    width: 100%;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      height: 80px;
      color: #7777;
    }
    .van-tab--active {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .placeholder {
      flex-shrink: 0; //不参与空间平方,就可以站位了
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
